package com.iter.devbox.couchbase;

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.N1qlQueryResult;
import com.couchbase.client.java.query.N1qlQueryRow;

public class CouchbaseApp {

	public static void main(String args[]) {
		//Connect to localhost
		Cluster cluster = CouchbaseCluster.create();
		//Cluster cluster = CouchbaseCluster.create("192.168.10.42");

		//Open the default bucket
		Bucket defaultBucket = cluster.openBucket("default");//连接到默认的Bucket，安装后默认有一个default的Bucket
		//Bucket beerSampleBucket = cluster.openBucket("default");

		//Storing Documents 存储文档
		/*JsonObject phone = JsonObject.empty()
				  .put("brand", "xiaomi")
				  .put("price", "1699元")
				  .put("version", "android")
				  .put("made", "China");
		JsonDocument phoneJsonDocument = JsonDocument.create("phone", phone);
		JsonDocument phoneJsonDocumentResponse = defaultBucket.upsert(phoneJsonDocument);//upsert存在就更新，否则插入
		System.out.println(phoneJsonDocumentResponse.content());
		
		JsonObject person = JsonObject.empty()
				  .put("name", "zhangsan")
				  .put("age", 30)
				  .put("sex", "male")
				  .put("job", "engineer")
				  .put("city", "beijing");
		JsonDocument personJsonDocument = JsonDocument.create("person", person);
		JsonDocument personJsonDocumentResponse = defaultBucket.upsert(personJsonDocument);
		System.out.println(personJsonDocumentResponse.content());
		
		//Retrieving Documents 检索文档
		JsonDocument phoneDoc = defaultBucket.get("phone");
		System.out.println(phoneDoc.content());
		
		JsonDocument personDoc = defaultBucket.get("person");
		System.out.println(personDoc.content());*/
		
		//Remove Documents 删除文档
		/*JsonDocument removePhone = defaultBucket.remove("phone");
		JsonDocument removePerson = defaultBucket.remove("person");
		System.out.println(removePhone.content());
		System.out.println(removePerson.content());*/
		
		/*defaultBucket
	    .async()
	    .get("phone")
	    .flatMap(new Func1<JsonDocument, Observable<JsonDocument>>() {
	        @Override
	        public Observable<JsonDocument> call(final JsonDocument loaded) {
	            loaded.content().put("age", 28);
	            return defaultBucket.async().upsert(loaded);
	        }
	    })
	    .subscribe(new Action1<JsonDocument>() {
	    	@Override
	        public void call(final JsonDocument updated) {
	            System.out.println("Updated: " + updated.content());
	            
	            
	            defaultBucket.close();
	    		cluster.disconnect();
	        }
	    });*/
		
		//创建indexs
		//String statement = "CREATE PRIMARY INDEX ON default";
		//String statement = "DROP PRIMARY INDEX ON default";
		//N1qlQuery.simple(index);
		
		//Index.createPrimaryIndex().on("default");
		/*UsingPath usingPath = Index.dropPrimaryIndex("default");
		System.out.println(usingPath.toString());*/
		
		//System.out.println(Index.buildIndex());
		String statement = "SELECT * FROM default WHERE made = 'China'";
		N1qlQuery query = N1qlQuery.simple(statement);
		N1qlQueryResult result = defaultBucket.query(query);
		
		/*for (N1qlQueryRow row : result) {
			JsonObject jsonObject = row.value().getObject("default");
			System.out.println(jsonObject.getString("brand"));
			System.out.println(jsonObject.getString("price"));
			System.out.println(jsonObject.getString("version"));
			System.out.println(jsonObject.getString("made"));
		}*/
		
		//Disconnect and clear all allocated resources 关闭连接释放资源
		defaultBucket.close();
		cluster.disconnect();
	}
}
